//
//  LLMyViewController.m
//  LoveLove
//
//  Created by wkf on 2019/3/27.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMyViewController.h"
#import "LLEditProfileViewController.h"
#import "LLMyHeaderTableViewCell.h"
#import "LLMyBodyTableViewCell.h"
#import "LLMyOrderViewController.h"
#import "LLNewsViewController.h"
#import "LLAccountViewController.h"
#import "LLiGveLikeViewController.h"
#import "LLFocusViewController.h"
#import "LLMyPublishViewController.h"
#import "LLWatchHistoryViewController.h"
#import "LLIntegralViewController.h"
#import "LLSettingVC.h"
//#import "LLMyPublishListViewController.h"
#import "LLMyPublishListViewController.h"
#import "QWebVC.h"
#import "LLLoginViewController.h"

@interface LLMyViewController ()<UINavigationControllerDelegate>
@property (copy, nonatomic) NSArray *titlesArray; //标题数组
@property (copy, nonatomic) NSArray *imgsArray;//图片数组

@property (nonatomic,strong) NSDictionary *data;

@property (nonatomic,strong) NSString *kefuPhone;
@end

@implementation LLMyViewController

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
    if ([LLUserInfo sharedUser].isLogin) {
        [self getData];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
     [self getPhone];
    self.navigationItem.title = @"昵称";
    self.titlesArray = @[@[@"我的点赞",@"关注"],@[@"已发布"],@[@"积分",@"联系客服"]];
    self.imgsArray = @[@[@"hea 拷贝-1",@"eye_200.32876712329px_1199858_easyicon.net-1"],@[@"1111"],@[@"coins_126.72446437469px_1192918_easyicon.net",@"phone_call_117.64826700899px_1222148_easyicon.net"]];
    // Do any additional setup after loading the view.
}

- (void)configTableView {
    self.needRefresh = NO;
    [super configTableView];
    [self titleView];
    self.titleLabel.text = self.userInfo.user_name;
    [self.leftButton setImage:[UIImage imageNamed:@""] forState:0];
    [self.rightButton setImage:[UIImage imageNamed:@"setting_155.31464174455px_1218200_easyicon.net"] forState:0];
    self.tableView.mj_h = LLScreenHeight-self.tabH-self.navH;
    self.tableView.top = self.navH;
    self.tableView.backgroundColor = LLWhiteColor;
    [self.tableView registerCellByClassName:@"LLMyHeaderTableViewCell"];
    [self.tableView registerCellByClassName:@"LLMyBodyTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titlesArray.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section ==2) {
        return 1;
    } else {
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 150 : 70.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0000001;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0000001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *viewHeaderInSection = [[UIView alloc] init];
    viewHeaderInSection.backgroundColor = LLCOLORBG;
    return viewHeaderInSection;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LLMyHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMyHeaderTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell refleshView];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        LLMyBodyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMyBodyTableViewCell"];
        cell.accessoryType = UITableViewCellSelectionStyleNone;
        cell.imageView.image = [UIImage imageNamed:self.imgsArray[indexPath.section-1][indexPath.row]];
        cell.textLabel.text = self.titlesArray[indexPath.section-1][indexPath.row];
        if (indexPath.section == 2) {
            NSLog(@"%@",[UIImage imageNamed:self.imgsArray[indexPath.section-1][indexPath.row]]);
        }
        cell.textLabel.textColor = LLColorRGB(0);
        cell.textLabel.font = LLRegularFont(15);
        cell.textLabel.frame = CGRectMake(40, 0, 200, 44);
        cell.subtitleLabel.hidden = NO;
        cell.subtitleLabel1.frame = CGRectMake(160, 47, LLScreenWidth-177, 12);

    
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![LLUserInfo sharedUser].isLogin) {
        LLLoginViewController *vc = [LLLoginViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    
    if (indexPath.section == 0) {
        LLEditProfileViewController *vc = [LLEditProfileViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            LLiGveLikeViewController *vc = [LLiGveLikeViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        } else {

            LLFocusViewController *vc = [LLFocusViewController new];
            [self.navigationController pushViewController:vc animated:YES];

        }
    } else if (indexPath.section == 2) {
            LLMyPublishListViewController *vc = [LLMyPublishListViewController new];
            [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 3) {

        if (indexPath.row == 0) {

            QWebVC *vc = [QWebVC new];
            vc.urlString = [NSString stringWithFormat:@"http://www.haoaih.com/h5/users/get_point?u_id=%@&num=%@",[LLUserInfo sharedUser].user_id,[LLUserInfo sharedUser].user_random];
            vc.showNavBar = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 1) {
            NSString *message = [NSString stringWithFormat:@"拨打客服电话:%@",self.kefuPhone];
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"拨号" message:message preferredStyle:UIAlertControllerStyleActionSheet];//更改边框颜色
//            ac.view.layer.borderColor = [UIColor redColor].CGColor;//更改边框线条粗细ac.view.layer.borderWidth = 5.0f;//更改其背景色
//            ac.view.layer.backgroundColor = [UIColor yellowColor].CGColor;
            UIAlertAction *ala1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"1111的实现方法");
                // 点击确定按钮时 要进行的操作可以写到这里
                NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.kefuPhone];
                // NSLog(@"str======%@",str);
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

            }];
        
            UIAlertAction *ala3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"3333的实现方法");
                
            }];
            [ac addAction:ala1];
           
            [ac addAction:ala3];//显示弹框
            [self presentViewController:ac animated:true completion:nil];
            
           

        } else {

    
        }
    }
}

- (void)getData {

    NSDictionary *dic = @{@"edu_id":@"18",
                          @"user_id":[LLUserInfo sharedUser].user_id,
                          @"user_random":[LLUserInfo sharedUser].user_random,
                          };
//    5168b38fbc6ab960ba1a618982c5ad84
    if (self.page == 1) {
        [self.models removeAllObjects];
    }

//    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {


        self.data = result[@"data"];
        [LLUserInfo sharedUser].user_avatar = self.data[@"user_avatar"];
        [LLUserInfo sharedUser].user_name = self.data[@"user_name"];
         [LLUserInfo sharedUser].user_random = self.data[@"user_random"];
        LLUserInfo *user = [[LLUserInfo alloc]initWithDic:self.data ];
        user.user_id = dic[@"user_id"];
        self.titleLabel.text = self.userInfo.user_name;
        [self.tableView reloadData];

//        [SVProgressHUD dismiss];

    } failure:^(NSString *msg) {

        [SVProgressHUD showErrorWithStatus:msg];
    }];
}

#pragma mark - 左上角的信息动作点击事件
- (void)didButtonWithleftButton{

    QWebVC *vc = [QWebVC new];
    vc.urlString = [NSString stringWithFormat:@"http://www.haoaih.com/h5/users/my_loven?u_id=%@&num=%@",[LLUserInfo sharedUser].user_id,[LLUserInfo sharedUser].user_random];
    vc.showNavBar = YES;
    [self.navigationController pushViewController:vc animated:YES];

//    LLNewsViewController *vc = [LLNewsViewController new];
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 右上角的动作点击事件
-(void)didButtonWithrightButton{
    if (![LLUserInfo sharedUser].isLogin) {
        LLLoginViewController *vc = [LLLoginViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    LLSettingVC *vc = [LLSettingVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)dealloc {
    self.navigationController.delegate = nil;
    [LLNotificationCenter removeObserver:self];
}
#pragma mark---获取电话号码
-(void)getPhone{
   
    NSDictionary *dic = @{@"edu_id":@"40"};
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.kefuPhone = result[@"data"][@"tel"];
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}
@end
